package com.example.lemis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lemis.model.domain.News;
import com.example.lemis.mapper.NewsMapper;
import com.example.lemis.model.dto.PageDTO;
import com.example.lemis.model.vo.NewsVO;
import com.example.lemis.service.INewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 管理新闻数据 服务实现类
 * </p>
 *
 * @author lxp
 * @since 2025-06-24
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements INewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public NewsVO news_add(News news) {
        newsMapper.news_add(news);
        LambdaQueryWrapper<News> newsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        newsLambdaQueryWrapper.eq(News::getId, news.getId());
        News news1 = newsMapper.selectOne(newsLambdaQueryWrapper);
        NewsVO newsVO = new NewsVO();
        BeanUtils.copyProperties(news1, newsVO);
        return newsVO;
    }

    @Override
    public Page<NewsVO> news_search(String title, String clazz, PageDTO pageDTO) {
        Page<NewsVO> page = new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize());
        page = newsMapper.news_search(page, title, clazz);
        return page;
    }
}
